package cn.zhj.base.adapter

import android.content.Context
import android.text.method.ScrollingMovementMethod
import android.util.SparseArray
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide

class CommonViewHolder : RecyclerView.ViewHolder{

    private var mViews: SparseArray<View>? = null
    private var mConvertView: View? = null
    private var mContext: Context? = null

    constructor(context: Context, itemView: View, parent:ViewGroup) : super(itemView){
        this.mContext = context
        this.mConvertView = itemView
        mViews = SparseArray<View>()
    }

    companion object{
        fun get(context: Context, parent: ViewGroup, layoutId: Int): CommonViewHolder {
            val itemView = LayoutInflater.from(context).inflate(layoutId, parent,false)
            return CommonViewHolder(context, itemView, parent)
        }
    }



    /**
     * 通过viewId获取控件
     *
     * @param viewId
     * @return
     */
    fun <T : View?> getView(viewId: Int): T {
        var view = mViews!![viewId]
        if (view == null) {
            view = mConvertView!!.findViewById(viewId)
            mViews!!.put(viewId, view)
        }
        return view as T
    }

    fun setText(viewId: Int, text: String?): CommonViewHolder {
        val tv = getView<TextView>(viewId)
        tv.text = text?:""
        return this
    }

    fun setTextScroll(viewId: Int): CommonViewHolder {
        val tv = getView<TextView>(viewId)
        tv.movementMethod = ScrollingMovementMethod.getInstance()
        return this
    }

    fun setTextCorlor(viewId: Int, colorId: Int): CommonViewHolder {
        val view = getView<TextView>(viewId)
        view.setTextColor(ContextCompat.getColor(mContext!!, colorId))
        return this
    }

    fun setVisiable(viewId: Int , mVisiable:Boolean) : CommonViewHolder {
        val view = getView<View>(viewId)
        if (mVisiable) {
            view.visibility = View.VISIBLE
        } else {
            view.visibility = View.GONE
        }
        return this
    }

    fun setImageUrl(viewId: Int , url :String): CommonViewHolder {
        val iv  = getView<ImageView>(viewId)
        Glide.with(mContext!!).load(url).into(iv)
        return this;
    }

    fun setImageResource(viewId: Int , resId :Int): CommonViewHolder {
        val iv  = getView<ImageView>(viewId)
        Glide.with(mContext!!).load(resId).into(iv)
        return this;
    }

    fun setOnClickListener(viewId: Int , listener:View.OnClickListener): CommonViewHolder {
        val view = getView<View>(viewId)
        view.setOnClickListener(listener)
        return this
    }

    fun setBackgroundCorlor(viewId: Int, colorId: Int): CommonViewHolder {
        val view = getView<View>(viewId)
        view.setBackgroundColor(ContextCompat.getColor(mContext!!, colorId))
        return this
    }

    fun setBackground(colorId: Int): CommonViewHolder {
        itemView.setBackgroundColor(ContextCompat.getColor(mContext!!, colorId))
        return this
    }

    fun setAlpha(alpha: Float): CommonViewHolder {
        itemView.alpha = alpha
        return this
    }


    fun setItemOnLongClickListener(longClickListener: View.OnLongClickListener): CommonViewHolder {
        itemView.setOnLongClickListener(longClickListener)
        return this
    }

    fun setItemClickble(clickble: Boolean): CommonViewHolder {
        itemView.isEnabled = clickble
        return this
    }

    fun setItemOnClickListener(listener: View.OnClickListener?): CommonViewHolder {
        mConvertView!!.setOnClickListener(listener)
        return this
    }
}